public class SingleLinkedList {
    static class ListNode{
        public int val;
        public ListNode next;
        public ListNode(int val){
            this.val=val;
        }
    }

        public ListNode head;

    public void createList(){
        ListNode listNode1=new ListNode(12);
        ListNode listNode2=new ListNode(21);
        ListNode listNode3=new ListNode(33);
        ListNode listNode4=new ListNode(41);
        ListNode listNode5=new ListNode(52);
        listNode1.next=listNode2;
        listNode2.next=listNode3;
        listNode3.next=listNode4;
        listNode4.next=listNode5;
        this.head=listNode1;

    }





    //头插法插入一个node
    public void addFirst(int data){
        ListNode node=new ListNode(data);
        node.next=head;
        head=node;
    }
    //尾插法
    public void addLast(int data){
        ListNode node=new ListNode(data);
        ListNode cur=head;
        if(head==null){
            head=node;
            return;
        }
        while (cur.next!=null){
            cur=cur.next;
        }
        cur.next=node;
    }
    //任意位置插入,第一个数据节点为0号下标
    public void addIndex(int index,int data){
        if(index<0||index>size()){
            return;
        }
        if(index==0){
            addFirst(data);
            return;
        }
        if(index==size()){
            addLast(data);
            return;
        }
        ListNode node=new ListNode(data);
        ListNode cur=head;
        int count=0;
        while (count!=index-1){
            count++;
            cur=cur.next;
        }
        node.next=cur.next;
        cur.next=node;
    }
    //查找是否包含关键字key是否在单链表当中
    public boolean contains(int key){
         ListNode cur=head;
         while (cur!=null){
             if (cur.val==key){
                 return  true;
             }
             cur=cur.next;
         }
         return false;
    }
    //删除第一次出现关键字为key的节点
    public void remove(int key) {
        if(head==null){
            return;
        }
        if(head.val==key){
            head=head.next;
            return;
        }
        ListNode cur = head;
        while (cur.next!= null) {
            cur = cur.next;
            if (cur.next.val == key) {
                cur.next=cur.next.next;
                return;
            }

        }
        if(cur==null){
            return;
        }

    }
    //删除所有值为key的节点
    public void removeAllKey(int key){
        if(head==null)return;
        ListNode cur=head.next;
        ListNode prev=head;
        while (cur!=null){
            if(cur.val==key){
                prev.next=cur.next;
                cur=cur.next;
            }else {
                prev=cur;
                cur=cur.next;
            }
        }
        if(head.val==key){
            head=head.next;
        }

    }

    //得到单链表的长度
    public int size(){
        ListNode cur=head;
        int count=0;
        while (cur!=null){
            count++;
            cur=cur.next;
        }
        return count;
    }
    //打印数组
    public void display(){
        ListNode cur=head;
        while (cur!=null){
            System.out.print(cur.val+" ");
            cur=cur.next;
        }
        System.out.println();
    }
    //从指定节点打印
    public void display(ListNode newHead){
        ListNode cur=newHead;
        while (cur!=null){
            System.out.print(cur.val+"");
            cur=cur.next;
        }
        System.out.println();
    }
    public void clear(){
        ListNode cur=head;
        while (cur!=null){
            ListNode curN=cur.next;
            cur.next=null;
            cur=curN;
        }
        head=null;
    }
}
